package de.baumann.browser.fragments

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.widget.LinearLayout
import android.widget.Toast
import de.baumann.browser.R
import de.baumann.browser.activitys.oc.*
import de.baumann.browser.adapter.OCBAdapter
import de.baumann.browser.api.net.vo.Coin
import de.baumann.browser.base.BaseOdinFragment
import de.baumann.browser.db.ETHWallet
import de.baumann.browser.iview.IOCBView
import de.baumann.browser.present.OCBPresenter
import de.baumann.browser.utils.*
import de.baumann.browser.utils.web3j.OdinWallet
import de.baumann.browser.utils.web3j.WalletDaoUtils
import de.baumann.browser.views.dialog.OcWalletAgreementDialog
import kotlinx.android.synthetic.main.activity_o_c_b.ocbList
import kotlinx.android.synthetic.main.activity_o_c_b.ocbdc
import kotlinx.android.synthetic.main.activity_o_c_b.qbdz
import kotlinx.android.synthetic.main.activity_o_c_b.qblb
import kotlinx.android.synthetic.main.activity_o_c_b.qbmc
import kotlinx.android.synthetic.main.fragment_home_wallet.*
import kotlinx.android.synthetic.main.include_tool_bar.*

class HomeWalletFragment() : BaseOdinFragment<IOCBView, OCBPresenter>(), IOCBView {

    companion object {
        val instance: HomeWalletFragment by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            HomeWalletFragment()
        }
    }

    var ethWallet: ETHWallet? = null
    private fun odinWallerData() {

        val waller = getMoneyAddress()
        if (waller == "") {
            llShow!!.visibility = View.VISIBLE
            llShow1!!.visibility = View.GONE
        } else {
            ethWallet = WalletDaoUtils.queryWalletByAddress(getMoneyAddress())
            llShow!!.visibility = View.GONE
            llShow1!!.visibility = View.VISIBLE
        }
        val a = getMoneyAddress()
        qbmc.text = WalletDaoUtils.getWalletName(waller)
        qbdz.text = StringUtil.StrReplace(waller)
        oCBAdapter = OCBAdapter(waller!!)
        ocbList.adapter = oCBAdapter
        oCBAdapter.setOnItemClickListener { _, _, position ->
            val coin = oCBAdapter.getItem(position)
            if (coin!!.name.toUpperCase() == "ODIN") {
                OcWalletDetailActivity.startOcWalletDetailActivity(mContext, ethWallet!!)
            } else {
                OCBDetailActivity.startOCBDetailActivity(mContext, this.ethWallet!!.name, ethWallet!!.address, coin.name, coin.node, coin.contract)
            }
        }
        ocbdc.setOnClickListener {
            if (a!!.startsWith("0x")) {
                StringUtil.copy(mContext, "odx${a!!.substring(2)}")
            } else {
                StringUtil.copy(mContext, a)
            }
            Toast.makeText(mContext, "已复制到剪切板", Toast.LENGTH_SHORT).show()
        }

        getPresenter().getList()
    }

    private lateinit var oCBAdapter: OCBAdapter
    override fun getLayout(): Int {
        return R.layout.fragment_home_wallet
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)
    }

    override fun initView(view: View) {

        commTitle.text = "Odin 钱包"
        (activity as AppCompatActivity).setSupportActionBar(commToolBar)
        val layoutParams = commToolBar.layoutParams as LinearLayout.LayoutParams
        layoutParams.setMargins(0, 0, 0, 0)
        qblb.setOnClickListener {
            OcWalletListActivity.startWalletManagerActivityForResult(this)
        }

        rlOcWalletCreate.setOnClickListener {
            OcWalletAgreementDialog(mContext).show()
        }
        rlImportWallet.setOnClickListener {
            ImportWalletActivity.startImportWalletActivity(mContext)
        }
    }

    override fun onResume() {
        super.onResume()
        odinWallerData()

    }


    override fun initData() {
    }

    private fun getWallet(): ETHWallet {
//        return intent.getSerializableExtra("odinWallet") as ETHWallet
        return getETHWallet()!!
    }


    override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
        inflater!!.inflate(R.menu.menu_wallet_setting, menu)
        super.onCreateOptionsMenu(menu, inflater)

    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        OcWalletSettingActivity.startOcWalletSettingActivityForResult(activity!!, ethWallet!!)
        return super.onOptionsItemSelected(item)
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            Log.d("11111", "onActivityResult: " + WalletDaoUtils.loadAll()[0])


//            odinWallerData()
        }
        Log.d("11111", "onActivityResult: " + WalletDaoUtils.loadAll()[0])
    }

    override fun createPresenter(): OCBPresenter? {
        return OCBPresenter()
    }

    override fun createView(): IOCBView? {
        return this
    }

    override fun setCoinList(list: List<Coin>) {
        oCBAdapter.setNewData(list)
    }

    override fun onDestroy() {
        super.onDestroy()
        OdinWallet.removeActivity(mContext as Activity?)
    }
}